/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.firewall;

import nfse.NetFSE;
import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.record.FlexRecord;
import nfse.gui.record.FlexRecordField;

import java.util.StringTokenizer;

public class IPFWRecord extends NetFSERecord {

    public int proto = -1;

    public int dstPort = -1;

    public int srcPort = -1;

    public long dstIP = -1;

    public String dstAddr = "";

    public String interfaceName = "";

    public String action = "";

    public String code = "";

    public String direction = "";

    public IPFWRecord() {

    }
    
    public static void main(String[] args) {
        IPFWRecord ipfw = new IPFWRecord();
        ipfw.parse(args, ipfw);
    }

    public int[] getIndexedFields() {
        int[] temp = { NetFSE.FIELD_START_TS, NetFSE.FIELD_SRC_IP, NetFSE.FIELD_DST_IP, NetFSE.FIELD_DST_PORT };
        return temp;
    }

    public long getDstIP() {
        return dstIP;
    }

    public int getDstPort() {
        return dstPort;
    }

    public void setDstIP(long dstIP) {
        this.dstIP = dstIP;
        this.dstAddr = NetFSEUtil.convertLongToIP(dstIP);
    }

    public IPFWRecord(int sensor) {
        this.setSensorID(sensor);
    }

    public boolean isSyslog() {
        return true;
    }

    public NetFSERecord generate(String line, int sensor) {
        try {
            // Record of the form: Jan 17 13:48:08 benjamin-uphoffs-computer
            // tag: 12190 Deny TCP 10.0.1.100:54765 10.0.1.195:2 in via en1
            IPFWRecord rec = new IPFWRecord(sensor);
            // int idx1 = line.indexOf(";");
            // int idx2 = line.indexOf(";", idx1 + 2);

            line = line.trim();

            StringTokenizer st = new StringTokenizer(line);

            // System.out.println(line);
            rec.setInitialString(line);
            rec.setStartTS(NetFSEUtil.timestampFromSyslog(line));

            String token = st.nextToken();
            while (!token.endsWith(":")) { // Advance to the end of the header
                token = st.nextToken();
            }

            st.nextToken();
            rec.action = st.nextToken();
            rec.proto = NetFSEUtil.getProtocol(st.nextToken());

            token = st.nextToken(); // the source info
            int idx = token.indexOf(":");
            rec.setSrcIP(NetFSEUtil.convertIPtoLong(token.substring(0, idx)));
            rec.srcPort = Integer.parseInt(token.substring(idx + 1));

            token = st.nextToken(); // the destination info
            idx = token.indexOf(":");
            rec.setDstIP(NetFSEUtil.convertIPtoLong(token.substring(0, idx)));
            rec.dstPort = Integer.parseInt(token.substring(idx + 1));

            rec.direction = st.nextToken();
            st.nextToken();
            rec.interfaceName = st.nextToken();

            return rec;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null;
    }

    public String getCreateFieldTableSQL(String table) {
        StringBuffer temp = new StringBuffer("create table " + table + " (ID bigint, ");
        temp.append("srcip inet,");
        temp.append("srcport int,");
        temp.append("dstip inet,");
        temp.append("dstport int,");
        temp.append("protocol int,");
        temp.append("action varchar(10),");
        temp.append("direction varchar(5),");
        temp.append("interface varchar(15),");
        temp.append("startts timestamp");
        temp.append(")");
        return temp.toString();
    }

    public FlexRecord getFlexRecord() {
        FlexRecord temp = new FlexRecord();
        temp.values = new String[9];
        int idx = -1;
        temp.values[++idx] = this.getTimestamp().toString();
        temp.values[++idx] = this.getSrcAddr();
        temp.values[++idx] = this.srcPort + "";
        temp.values[++idx] = this.dstAddr;
        temp.values[++idx] = this.dstPort + "";
        temp.values[++idx] = this.proto + "";
        temp.values[++idx] = this.action + "";
        temp.values[++idx] = this.direction + "";
        temp.values[++idx] = this.interfaceName + "";
        return temp;
    }

    public FlexRecordField[] getFlexRecordFields() {
        FlexRecordField[] temp = new FlexRecordField[9];
        int idx = -1;
        temp[++idx] = new FlexRecordField("Timestamp", "startts", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_TIMESTAMP);
        temp[++idx] = new FlexRecordField("Src IP", "srcip", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_IP_ADDR);
        temp[++idx] = new FlexRecordField("Src Port", "srcport", FlexRecordField.TYPE_INT,
                FlexRecordField.SUB_TYPE_PORT);
        temp[++idx] = new FlexRecordField("Dst IP", "dstip", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_IP_ADDR);
        temp[++idx] = new FlexRecordField("Dst Port", "dstport", FlexRecordField.TYPE_INT,
                FlexRecordField.SUB_TYPE_PORT);
        temp[++idx] = new FlexRecordField("Protocol", "protocol", FlexRecordField.TYPE_INT,
                FlexRecordField.SUB_TYPE_PROTOCOL);
        temp[++idx] = new FlexRecordField("Action", "action", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Direction", "direction", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Interface", "interface", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        return temp;
    }

    public String getInsertFieldTableSQL(int id, String table) {
        StringBuffer temp = new StringBuffer("insert into " + table + " values (" + id + "");
        temp.append(",'");
        temp.append(getSrcAddr() + "'");
        temp.append(",");
        temp.append(srcPort + "");
        temp.append(",'");
        temp.append(dstAddr + "'");
        temp.append(",");
        temp.append(dstPort);
        temp.append(",");
        temp.append(proto);
        temp.append(",");
        temp.append("'" + action + "'");
        temp.append(",");
        temp.append("'" + direction + "'");
        temp.append(",");
        temp.append("'" + interfaceName + "'");
        temp.append(",");
        temp.append("'" + getTimestamp().toString() + "'");
        temp.append(")");
        return temp.toString();
    }

}